﻿<%@ Page Title="" Language="C#" MasterPageFile="~/PageMaster.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MonitorPlan.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title>
        Welcome to Mesh Technology - House Security Control
    </title>
    <style>
        .box-holder{
            margin: 22px auto;
            text-align: center;
            padding: 32px 4px;
            width: 70%;
        }
        #txtId{
            width: 90%;
            padding: 9px;
        }
    </style>
    <script>
        $(document).ready(function () {
            // send email if cookie is not empty
            if ($.cookie("houseid") != "" && $.cookie("key") != "")
            {
                // alarm sound
                getAlarm($.cookie("houseid"), $.cookie("key"));
                // send email to client
                sendEmail($.cookie("houseid"), $.cookie("key"));
                // send sms
                sendSms($.cookie("houseid"), $.cookie("key"));
                // clear cookies
                $.removeCookie("houseid");
                $.removeCookie("key");
            }
            // hide message box at the first time
            $("#smsbox").hide();
            $(this).on("click", function () {
                // auto focus on text box even user clicks anywhere
                $("#txtId").focus();
            });
            // focus text box after the document loading finished
            $("#txtId").focus();
            // bind key event to the text box
            $("#txtId").on("keyup", function (e) {
                var strkey = this.value;
                var tkey = strkey.charAt(0);
                var hid = $("#ContentPlaceHolder1_HiddenField1").val();
                // if key "-" is triggered, play alarm sound
                if (tkey == "-")
                    alarm();
                // if other key is triggered, do transaction
                else {
                    beginTransaction(hid, tkey);
                    
                }
                 // clear textbox after transaction
                $(this).val("");
                
            });
        });
        // function to play alarm sound
        function alarm() {
            var sound = document.getElementById("sound");
            sound.src = "img/alarm.mp3";
            sound.play();
        }
        // set alarm
        function getAlarm(houseid, keyonoff)
        {
            // make ajax request to the web method Alarm
            $.ajax({
                type: "POST",
                async: true,
                url: "Transaction.aspx/Alarm",
                data: "{hid:'" + houseid + "', key:'" + keyonoff + "' }",
                contentType: "application/json",
                dataType: "json",
                success: function (response) {
                    if(response.d=="OK")
                    {
                        alarm();
                    }
                },
                failure: function (response) {

                }
            });
        }
        // send email
        function sendEmail(houseid, keyonoff)
        {
            // send ajax request to web method SendEmail
            $.ajax({
                type: "POST",
                async: true,
                url: "Transaction.aspx/SendEmail",
                data: "{hid:'" + houseid + "', key:'" + keyonoff + "' }",
                contentType: "application/json",
                dataType: "json",
                success: function (response) {  },
                failure: function (response) { }
            });
        }
        // transaction request
        function beginTransaction(houseid, keyonoff)
        {
            // remove house id and key form cookie
            $.removeCookie("houseid");
            $.removeCookie("key");
            // make ajax request to TranSact web method
            $.ajax({
                type: "POST",
                async: true,
                url: "Transaction.aspx/TranSact",
                data: "{hid:'" + houseid+"', key:'"+keyonoff+"' }",
                contentType: "application/json",
                dataType: "json",
                success: function (response) {
                    $("#sms").html(response.d);
                    $("#smsbox").show(200).delay(1000).fadeOut("slow", function () {
                        // set houseid and key to cookie for using to send email and alarm
                        $.cookie("houseid", houseid);
                        $.cookie("key", keyonoff);
                        location.reload();
                        
                    });
                    
                },
                failure: function (response) {
                    $("#sms").html(response.d);
                    $("#smsbox").show(200).delay(2000).fadeOut("slow");
                }
            });

          
        }
        // request sms info before sending
        function sendSms(houseid, keyonoff) {
            $.ajax({
                type: "POST",
                async: true,
                url: "Transaction.aspx/SendSms",
                data: "{hid:'" + houseid + "', key:'" + keyonoff + "' }",
                contentType: "application/json",
                dataType: "json",
                success: function (response) {
                    // send message here...
                    sendMessage(response.d);
                }
            });
        }
        // function to send sms
        function sendMessage(info)
        {
            if (info != "no sms" && info != "no phone") {
                var str = info.split(":");
                var str1 = str[0];
                var str2 = str[1];
                ///////////////////////////////////
                var tool = str1.split(",");
                var phone = str2.split(",");

                var toPhone, onOff, toolId, toolName;
                toolId = tool[0];
                toolName = tool[1];
                onOff = tool[2];
                for(var i=0;i<phone.length;i++)
                {
                    if(phone[i]!="")
                    {
                        toPhone = phone[i];
                        $.ajax({
                           // type: "GET",
                            crossOrigin: true,
                            url: "http://localhost/json/sms.php?toolId="+toolId+"&toolName="+toolName+"&onoff="+onOff+"&to="+toPhone,
                            //dataType: "jsonp",
                            //crossDomain: true,
                            success: function (data) {
                               // alert(data);
                            },
                            failure: function(data){ }
                        });
                    }
                }
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="container">
       <div class="box-holder">
           <div class="row" style="text-align:left">
               <input type="text" id="txtId" />
               <input type="hidden" runat="server" ID="HiddenField1" runat="server" />
               <asp:Label ID="lb" runat="server"></asp:Label>
           </div>
           <div class="row">
               <div class='alert alert-warning' id="smsbox" style="text-align:left; width:80%">
                   <button type='button' class='close' data-dismiss='alert'>&times;</button>
                    <div id="sms"></div>
               </div>
           </div>
           <div class="row" style="text-align:left">
               <h5 class="text-info">Transaction Today</h5>
               <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
               <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                   <ContentTemplate>
                         <asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ ConnectionStrings:conStr %>"
                    SelectCommand=""></asp:SqlDataSource>
               <asp:Repeater ID="rp1" runat="server" DataSourceID="ds1">
                   <HeaderTemplate>
                   <table class="table table-condensed table-striped table-hover">
                                <tr>
                                    <th>Tool Id</th>
                                    <th>Tool Name</th>
                                    <th>Date</th>
                                    <th>Time</th>
                                    <th>Status</th>
                                </tr>
                        </HeaderTemplate>
                   <ItemTemplate>
                       <tr>
                           <td><%# Eval("toolid") %></td>
                           <td><%# Eval("toolname") %></td>
                           <td><%# Eval("date") %></td>
                           <td><%# Eval("time") %> <%# Eval("ampm") %></td>
                           <td><img src='img/<%# Eval("light") %>' width='27' /></td>
                       </tr>
                   </ItemTemplate>
                   <FooterTemplate>
                       </table>
                   </FooterTemplate>

                   
               </asp:Repeater>
                   </ContentTemplate>
               </asp:UpdatePanel>
           </div>
       </div>
        <audio id="sound">
        </audio>
    </div>
    
</asp:Content>
